package com.xzx.study.service;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.android.internal.telephony.ITelephony;
import com.xzx.study.R;

import java.lang.reflect.Method;

public class ServiceActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "TAG-" + ServiceActivity.class.getSimpleName();
    private Button play;
    private Button stop;
    private Button pause;
    private Button exit;
    private Button end_call;

    private MediaPlayer mediaPlayer;

    // 要申请的权限
    private String[] permissions = {Manifest.permission.MODIFY_PHONE_STATE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        play = findViewById(R.id.play);
        stop = findViewById(R.id.stop);
        pause = findViewById(R.id.pause);
        exit = findViewById(R.id.exit);
        end_call = findViewById(R.id.end_call);

        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        pause.setOnClickListener(this);
        exit.setOnClickListener(this);
        end_call.setOnClickListener(this);

        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Log.i(TAG, "系统版本大于23");
            // 检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            if (i != PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "没有授权");
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, permissions, 321);
            }
       }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MusicService.class);
        if (play == v) {
            //            playMusic();
            intent.putExtra("action", "play");
            startService(intent);
        } else if (stop == v) {
            //            stopMusic();
            intent.putExtra("action", "stop");
            startService(intent);
        } else if (pause == v) {
            //            pauseMusic();
            intent.putExtra("action", "pause");
            startService(intent);
        } else if (exit == v) {
            //            exitMusic();
            stopService(intent);
            finish();
        } else if (end_call == v) {
            end_call();
        }
    }

    private void end_call() {

        try {
            Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
            IBinder binder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE);
            ITelephony telephony = ITelephony.Stub.asInterface(binder);
            telephony.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopMusic();
    }

    private void exitMusic() {
        Log.i(TAG, "退出音乐播放");
        finish();
    }

    private void pauseMusic() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    private void stopMusic() {
        if (mediaPlayer != null) {
            // 停止音乐有3步
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    private void playMusic() {
        Log.i(TAG, "播放音乐");
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.water_hander);
        }
        mediaPlayer.start();
    }
}
